package com.cino.org.service.impl;

import com.cino.basic.base.exception.BusinessException;
import com.cino.basic.base.service.impl.BaseServiceImpl;
import com.cino.basic.util.BaiduAiUtils;
import com.cino.basic.util.Md5Utils;
import com.cino.basic.util.StrUtils;
import com.cino.org.domain.Employee;
import com.cino.org.domain.Shop;
import com.cino.org.domain.ShopAuditLog;
import com.cino.org.dto.ShopDto;
import com.cino.org.mapper.EmployeeMapper;
import com.cino.org.mapper.ShopAuditLogMapper;
import com.cino.org.mapper.ShopMapper;
import com.cino.org.service.ShopService;
import com.cino.users.domain.Logininfo;
import com.cino.users.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) //有事务就只有1个
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        System.out.println(shop);
        //一.校验
        //1.空值校验
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())){
            throw new BusinessException("信息不能为空!!");
        }
        //2.两次密码不一致校验
        if(!shop.getAdmin().getConfirmPassword().equals(shop.getAdmin().getPassword())){
            throw new BusinessException("两次密码输入不一致!!!");
        }
        //3.当前店铺是否入驻过
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if(dbShop != null){//找到了
            throw new BusinessException("该店铺已入驻，请直接登录或联系管理员");
        }

        //二.百度AI智能审核【店铺名，logo是否合法】
            if(!BaiduAiUtils.TextCensor(shop.getName())){
                throw new BusinessException("店铺名不合法！！");
            }

            if(!BaiduAiUtils.ImgCensor("http://123.207.27.208"+ shop.getLogo())){
                throw new BusinessException("店铺logo不合规！！");
            }



        //三:添加到数据库
        //1.先添加员工信息
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);//shop_id =null 会返回自增id3个属性
        //2.再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);//这里会返回自增id
        //3.将店铺id更新到员工表中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
        //四:人工审核之后发送激活邮件

        //========================重构，加密加盐====================================
        //添加logininfo信息
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.save(logininfo);

    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog log) throws MessagingException {
        Shop shop = shopMapper.loadById(log.getShopId());
        //1.修改状态为通过
        shopMapper.updateState(2,log.getShopId());
        //2.添加审核日志
        log.setState(1);
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("928915144@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://127.0.0.1:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>激活邮件</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送1
        javaMailSender.send(mimeMessage);

    }

    @Override
    @Transactional
    public void auditReject(ShopAuditLog log) throws MessagingException {
        Shop shop = shopMapper.loadById(log.getShopId());
        //1.修改状态为未审核通过
        shopMapper.updateState(4,log.getShopId());
        //2.添加审核日志
        log.setState(0);
        log.setAuditId(350L);//先写死，后续需要登录的人的id
        shopAuditLogMapper.save(log);

        //3.发邮件
        //true - 能够添加附件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //发件人
        simpleMailMessage.setFrom("928915144@qq.com");
        simpleMailMessage.setSubject("店铺激活失败通知");
        simpleMailMessage.setText("激活失败");
        //店铺管理员
        Employee admin = employeeMapper.loadById(shop.getAdminId());
        simpleMailMessage.setTo(admin.getEmail());
        //发送
        javaMailSender.send(simpleMailMessage);

    }

    //激活的链接 修改状态
    @Override
    @Transactional
    public void updateState(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(3);
        shopMapper.update(shop);
    }

    @Override
    public List<ShopDto> echarts() {
       return shopMapper.echarts();
    }

}
